package com.william.jdbcplus.boot.test.controller;

import com.william.jdbcplus.boot.starter.util.DaoUtil;
import com.william.jdbcplus.boot.test.dto.R;
import com.william.jdbcplus.boot.test.dto.ResultCode;
import com.william.jdbcplus.boot.test.entity.SysDemo;
import com.william.jdbcplus.boot.test.enums.TimeUnitEnum;
import com.william.jdbcplus.boot.test.service.SysDemoService;
import com.william.jdbcplus.common.page.IPage;
import com.william.jdbcplus.common.page.Page;
import com.william.jdbcplus.core.query.EntityWrap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * @author niliwei
 */
@Api(value = "实例测试")
@RestController
@AllArgsConstructor
@Validated
@RequestMapping("/demo")
@Slf4j
public class DemoController {

    private SysDemoService service;

    @ApiOperation(value = "业务创建", notes = "业务创建", produces = "application/json")
    @PostMapping(value = "/add")
    public R<String> add(@RequestBody @Valid SysDemo demo) {
        DaoUtil.BASE.add(demo);
        return R.success("成功");
    }

    @ApiOperation(value = "根据id修改", notes = "根据id修改", produces = "application/json")
    @PostMapping(value = "/editById")
    public R<String> editById(@RequestBody @Valid SysDemo demo) {
        try {
            demo.setTimeUnit(TimeUnitEnum.MINUTE);
            DaoUtil.BASE.updateById(demo);
            return R.success("成功");
        } catch (Exception e) {
            log.error("根据id修改业务模块异常", e);
            return R.fail(ResultCode.FAILURE);
        }
    }

    @ApiOperation(value = "分页列表查询", notes = "分页列表查询")
    @PostMapping(value = "/page")
    public R<IPage<SysDemo>> queryPage() {
        EntityWrap<SysDemo> wrap = new EntityWrap<>(SysDemo.class);
        wrap.lambda().eq(SysDemo::getTimeUnit, TimeUnitEnum.HOUR);
        IPage<SysDemo> page = new Page<>(1, 10);
        IPage<SysDemo> pageList = DaoUtil.BASE.page(page, wrap);
        return R.data(pageList);
    }

    @ApiOperation(value = "查询列表", notes = "根据条件查询列表数据")
    @PostMapping(value = "/queryList")
    public R<List<SysDemo>> queryList() {
        EntityWrap<SysDemo> wrap = new EntityWrap<>(SysDemo.class);
        wrap.lambda().eq(SysDemo::getTimeUnit, TimeUnitEnum.HOUR);
        List<SysDemo> list = DaoUtil.BASE.list(wrap);
        return R.data(list);
    }


    @ApiOperation(value = "根据ids进行事务测试", notes = "根据ids进行事务测试")
    @DeleteMapping(value = "/testTransactional")
    public R<String> testTransactional(@RequestParam(name = "ids", required = true) String ids) {
        try {
            String[] idSplit = ids.split(",");
            List<String> pkList = Arrays.asList(idSplit);
            // 删除多条数据
            service.testTransactional(pkList);
            return R.success("进行事务测试成功");
        } catch (Exception e) {
            log.error(e.toString(), e);
            return R.fail("进行事务测试失败，原因：" + e.toString());
        }
    }
}
